一、以Boolean為對象的兩個方法:
Boolean.prototype.toString()
「字串內容」取決於Boolean是true還是false,這個方法會覆蓋Object.prototype.toString()
Boolean.prototype.valueOf()
Boolean.prototype.valueOf()返回布林物件的原始型別,覆蓋Object.prototype.valueOf()
二、方法一 Boolean.prototype.toString()
對Boolean使用 toString() 會還傳一個表達布林值字串:
const flag1 = new Boolean(true);
console.log(flag1.toString()); // Expected output: "true"
const flag2 = new Boolean(1);
console.log(flag2.toString()); // Expected output: "true"
創建原本是false的布林物件:
const bNoParam = new Boolean(); //Boolean {false}
const bZero = new Boolean(0); //Boolean {false}
const bNull = new Boolean(null); //Boolean {false}
const bEmptyString = new Boolean(""); //Boolean {false}
const bfalse = new Boolean(false); //Boolean {false}
創建原本是true的布林物件:
const btrue = new Boolean(true); //Boolean {true}
const btrueString = new Boolean("true"); //Boolean {true}
const bfalseString = new Boolean("false"); //Boolean {true}
const bSuLin = new Boolean("Su Lin"); //Boolean {true}
const bArrayProto = new Boolean([]); //Boolean {true}
const bObjProto = new Boolean({}); //Boolean {true}
//空物件和陣列都是true
Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/toString